/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package metro.jme.lottery.persist;

import metro.jme.lottery.util.StringUtil;

/**
 * This class handles the storage of the favourite set
 * @author pierre
 */
public abstract class FavBean {

	private static int id = 0;
	private static int set_id;

	/**
	 * 
	 * @param index 
	 */
	public static void store(int index) {
		// We got the index of the set we want to add as fav, so if we want to
		// save the right index, we have to look for it !
		System.out.println("id = " + index);
		System.out.println(SetBean.toStringS());

		// Get the setbean at index : index
		SetBean sb = SetBean.getBeans()[index];
		String s = "" + set_id;
		set_id = sb.getId();
		
		
		if (FavBean.id == 0) {
			// First storage
			FavBean.id = RMSUnit.storeFavouriteSet(s.getBytes(), true);
		} else {
			// Overwrite old set_id if one exists
			RMSUnit.storeFavouriteSet(s.getBytes(), false);
		}

	}

	/**
	 * 
	 * @return 
	 */
	public static String getFavSet() {
		String fav = null;
		if (id > 0 && set_id > 0) {
			fav = SetBean.getBean(set_id).getValue();
		}
		System.out.println("fav = " + fav);
		return fav;
	}

	/**
	 * 
	 * @return 
	 */
	public static int[] getFavSetAsArray() {
		String fav = getFavSet();
		int[] result = StringUtil.explode(fav, ' ');
		return result;
	}
	
	/**
	 * 
	 * @return 
	 */
	public static int getFavIndex() {
		int index = 0;
		boolean found = false;
		System.out.println("getFavIndex");
		System.out.println("set_id = " + set_id);
		if (set_id > 0) {
			SetBean[] list = SetBean.getBeans();
			int i = 0;
			while( i < list.length && found == false) {
				if (list[i].getId() == set_id) {
					index = i;
					found = true;
				}
				i++;
			}
		}
		System.out.println("index = " + index);
		return index;
	}
}
